package com.jikangjun.disk.recycle.service.impl;

import com.jikangjun.disk.mybatis.flexigrid.PagingEntity;
import com.jikangjun.disk.mybatis.flexigrid.TblResponseEntity;
import com.jikangjun.disk.recycle.entity.RecycleFileEntity;
import com.jikangjun.disk.recycle.entity.SearchRecycleEntity;
import com.jikangjun.disk.recycle.repository.RecycleDao;
import com.jikangjun.disk.recycle.service.RecycleService;
import com.jikangjun.disk.spring.security.UserDiskEntity;
import com.jikangjun.disk.spring.security.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Service
public class RecycleServiceImpl implements RecycleService {
    @Resource
    private RecycleDao recycleDao;

    @Override
    public TblResponseEntity<RecycleFileEntity> queryRecycleFilesByPage(SearchRecycleEntity entity, PagingEntity paging) {
        // 获取当前登录用户
        UserDiskEntity currentUser = SecurityUtils.getCurrentUser();
        entity.setUserId(currentUser.getUserUuid());

        System.out.println("有效时间参数：" + entity.getValidTime());

        TblResponseEntity<RecycleFileEntity> response = new TblResponseEntity<>();
        int total = recycleDao.queryRecycleCount(entity);
        response.setTotal(total);

        paging.deal(RecycleFileEntity.class, total);
        response.setPage(paging.getPage());

        List<RecycleFileEntity> fileList = recycleDao.queryRecycleFilesByPage(entity, paging);
        response.setRawRecords(fileList);

        response.deal();
        return response;
    }

    @Override
    @Transactional
    public void recoverFile(String fileId) {
        UserDiskEntity currentUser = SecurityUtils.getCurrentUser();
        RecycleFileEntity recycleFile = recycleDao.queryById(fileId, currentUser.getUserUuid());

        if (recycleFile == null) {
            throw new RuntimeException("文件不存在或没有操作权限");
        }
        // 恢复文件逻辑：将文件从回收站移回原位置
        // 1. 恢复文件表记录
        // 2. 删除回收站记录
        recycleDao.recoverFile(recycleFile);
        recycleDao.deleteById(fileId, currentUser.getUserUuid());
    }

    @Override
    @Transactional
    public void batchRecoverFiles(String fileIds) {
        UserDiskEntity currentUser = SecurityUtils.getCurrentUser();
        List<String> ids = Arrays.asList(fileIds.split(","));

        // 批量恢复文件
        List<RecycleFileEntity> recycleFiles = recycleDao.queryByIds(ids, currentUser.getUserUuid());
        if (recycleFiles.size() != ids.size()) {
            throw new RuntimeException("部分文件不存在或没有操作权限");
        }

        recycleDao.batchRecoverFiles(recycleFiles);
        recycleDao.batchDeleteByIds(ids, currentUser.getUserUuid());
    }

    @Override
    @Transactional
    public void permanentDelete(String fileIds) {
        UserDiskEntity currentUser = SecurityUtils.getCurrentUser();
        List<String> ids = Arrays.asList(fileIds.split(","));

        // 1. 删除物理文件（实际应用中需要根据存储策略删除）
        // 2. 删除回收站记录
        recycleDao.batchDeleteByIds(ids, currentUser.getUserUuid());
    }

    @Override
    @Transactional
    public void emptyRecycleBin() {
        UserDiskEntity currentUser = SecurityUtils.getCurrentUser();
        // 1. 删除当前用户回收站所有物理文件
        // 2. 清空回收站记录
        recycleDao.emptyRecycleBin(currentUser.getUserUuid());
    }
}